<?php 
/**
 * ReadPress是一个优秀的主题，回归纯粹的博客，文艺、阅读、写作、摄影、展示wordpress主题
 * Gitee：https://gitee.com/MUCEO/readpress/
 * 作者唯一QQ：858896214 （秋知德雨）
 * QQ交流群：917367358
 * 开发者不易，感谢支持，如果使用本主题尽量留个版权或者链接
 */
?>
<?php
ini_set("error_reporting","E_ALL & ~E_NOTICE");
	if (! function_exists('_read')) {
    function _read($option = '', $default = null) {
        $options = get_option('readpre_my_options');
        return (isset($options[$option])) ? $options[$option] : $default;
}}
require get_template_directory() . '/inc/readpressShelf/readpressfk.php';
function wpmaker_menu_classes($classes, $item, $args) {
    if ($args->theme_location == 'nav') {
        $classes[] = 'nav-s';
    }
    return $classes;
}
add_filter('nav_menu_css_class','wpmaker_menu_classes', 2, 3);
require get_template_directory() . '/inc/seo.php';
require get_template_directory() . '/inc/yh.php';
require get_template_directory() . '/inc/htyh.php';
require get_template_directory() . '/inc/rp_translate_id.php';
require get_template_directory() . '/inc/editor.php';
function post_thumbnail_srcs(){
	global $post;
	if( $values = get_post_custom_values("thumb") ) {
		$values = get_post_custom_values("thumb");
		$post_thumbnail_src = $values [0];
	} elseif( has_post_thumbnail() ){
		$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
		$post_thumbnail_src = $thumbnail_src [0];
	} else {
		$post_thumbnail_src = '';
		ob_start();
		ob_end_clean();
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		if(!empty($matches[1][0])){
			$post_thumbnail_src = $matches[1][0]; 
		}else{
			$random = mt_rand(1, 5);
			$post_thumbnail_src = get_template_directory_uri().'/img/me/'.$random.'.jpg';
		}
	};
	echo $post_thumbnail_src;
}
 register_nav_menus( array(
'nav' => '电脑端导航', 
'tags' => '移动端导航'
) );
add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
function my_css_attributes_filter($var) {
return is_array($var) ? array_intersect($var, array('layui-nav-item','layui-nav','nav-3333333333333item','menu-item-has-children','current-menu-item','layui-this')) : '';
}
function codedocs_add_classes_on_li($classes) { $classes[] = 'layui-nav-item'; return $classes;
}add_filter( 'nav_menu_css_class', 'codedocs_add_classes_on_li' );
class Header_Menu_Walker extends Walker_Nav_Menu{	
function start_lvl(&$output, $depth = 0, $args = array() ) {
	$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' );
    $display_depth = ( $depth + 1);
 
	$classes = array( '', 
	( $display_depth % 2 ? 'menu-odd' : 'menu-even' ), 
	( $display_depth =1 ? '' : '' ), 
	);
	$class_names = implode( ' ', $classes ); 
 
	$output .= "\n" . $indent . '<ul class="layui-nav-child layui-anim layui-anim-upbit">' . "\n";
}
function end_lvl(&$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>";
}
}
add_filter( 'show_admin_bar', '__return_false' );
function lingfeng_pagenavi( $range = 4 ) {
    global $paged,$wp_query;
    $max_page='';
    if ( !$max_page ) {
        $max_page = $wp_query->max_num_pages;
    }
    if( $max_page >1 ) {
        echo "<div class='fenye'>"; 
        if( !$paged ){
            $paged = 1;
        }
        if( $paged != 1 ) {
            echo "<a href='".get_pagenum_link(1) ."' class='extend' title='跳转到首页'><<</a>";
        }
        previous_posts_link('<');
        if ( $max_page >$range ) {
            if( $paged <$range ) {
                for( $i = 1; $i <= ($range +1); $i++ ) {
                    echo "<a href='".get_pagenum_link($i) ."'";
                if($i==$paged) echo " class=\"current\"";echo ">$i</a>";
                }
            }elseif($paged >= ($max_page -ceil(($range/2)))){
                for($i = $max_page -$range;$i <= $max_page;$i++){
                    echo "<a href='".get_pagenum_link($i) ."'";
                    if($i==$paged)echo " class=\"current\"";echo ">$i</a>";
                    }
                }elseif($paged >= $range &&$paged <($max_page -ceil(($range/2)))){
                    for($i = ($paged -ceil($range/2));$i <= ($paged +ceil(($range/2)));$i++){
                        echo "<a href='".get_pagenum_link($i) ."'";if($i==$paged) echo " class=\"current\"";echo ">$i</a>";
                    }
                }
            }else{
                for($i = 1;$i <= $max_page;$i++){
                    echo "<a href='".get_pagenum_link($i) ."'";
                    if($i==$paged)echo " class=\"current\"";echo ">$i</a>";
                }
            }
        next_posts_link('>');
        if($paged != $max_page){
            echo "<a href='".get_pagenum_link($max_page) ."' class='extend' title='跳转到最后一页'>>></a>";
        }
        echo '<span>['.$max_page.']</span>';
        echo "</div>\n";  
}
}
function comment_admin_title($comment){
if($comment == 1){
   return '<span class="bb-comment isauthor" title="作者">博主</span>';}}
   function comment_admin_title_admin($comment){
if($comment == 1){
   return 'adminhighlight ';}}
function simple_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li class="comment" id="comment-<?php comment_ID(); ?>">
<div class="media">
<div class="media-left">
<?php if (function_exists('get_avatar') && get_option('show_avatars')) { echo get_avatar($comment, 48); } ?>
</div>
<div class="media-body">
<?php printf(__('<p class="'.comment_admin_title_admin($comment->user_id).'author_name">%s'.comment_admin_title($comment->user_id).'</p>'), get_comment_author_link()); ?>
<?php if ($comment->comment_approved == '0') : ?>
<em>评论等待审核...</em><br />
<?php endif; ?>
<?php comment_text(); ?>
</div>
</div>
<div class="comment-metadata">
<span class="comment-pub-time">
<?php echo get_comment_time('Y-m-d H:i');
if ( current_user_can('level_10') ) {
$url = home_url();
echo '<a id="delete-'. $comment->comment_ID .'" href="' . wp_nonce_url("$url/wp-admin/comment.php?action=deletecomment&p=" . $comment->comment_post_ID . '&c=' . $comment->comment_ID, 'delete-comment_' . $comment->comment_ID) . '" >&nbsp;删除</a>
<a id="delete-'. $comment->comment_ID .'" href="' . wp_nonce_url("$url/wp-admin/comment.php?action=editcomment&c=" . $comment->comment_post_ID . '&c=' . $comment->comment_ID, 'delete-comment_' . $comment->comment_ID) . '" >&nbsp;编辑</a>';
} ?>
</span>

<span class="comment-btn-reply">
<i class="fa fa-reply"></i> <?php comment_reply_link(array_merge( $args, array('reply_text' => '回复','depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</span>
</div>
<?php } 
    if ( ! function_exists( 'get_cravatar_url' ) ) {
        function get_cravatar_url( $url ) {
            $sources = array(
                'www.gravatar.com',
                '0.gravatar.com',
                '1.gravatar.com',
                '2.gravatar.com',
                'secure.gravatar.com',
                'cn.gravatar.com',
                'gravatar.com',
            );
            return str_replace( $sources, 'cravatar.cn', $url );
        }
        add_filter( 'um_user_avatar_url_filter', 'get_cravatar_url', 1 );
        add_filter( 'bp_gravatar_url', 'get_cravatar_url', 1 );
        add_filter( 'get_avatar_url', 'get_cravatar_url', 1 );
    }
    if ( ! function_exists( 'set_defaults_for_cravatar' ) ) {
        function set_defaults_for_cravatar( $avatar_defaults ) {
            $avatar_defaults['gravatar_default'] = 'Cravatar 标志';
            return $avatar_defaults;
        }
        add_filter( 'avatar_defaults', 'set_defaults_for_cravatar', 1 );
    }
    if ( ! function_exists( 'set_user_profile_picture_for_cravatar' ) ) {
        function set_user_profile_picture_for_cravatar() {
            return '<a href="https://cravatar.cn" target="_blank">您可以在 Cravatar 修改您的资料图片</a>';
        }
        add_filter( 'user_profile_picture_description', 'set_user_profile_picture_for_cravatar', 1 );
    }
function comments_switch(){
    if(!empty(_rp('rp-index-comments-switch'))){
        echo  comments_template( $file, $separate_comments );
    }
}
    function record_visitors()  
    {
        if (is_singular())  
        {  
          global $post;  
          $post_ID = $post->ID;  
          if($post_ID)  
          {
              $post_views = (int)get_post_meta($post_ID, 'views', true);  
              if(!update_post_meta($post_ID, 'views', ($post_views+1)))  
              {  
                add_post_meta($post_ID, 'views', 1, true);  
              }  
          }  
        }  
    }  
    add_action('wp_head', 'record_visitors');  
    function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)  
    {
      global $post;  
      $post_ID = $post->ID;  
      $views = (int)get_post_meta($post_ID, 'views', true);  
      if ($echo) echo $before, number_format($views), $after;  
      else return $views;  
}
add_theme_support('post-thumbnails', array(
'post',
'page',
'custom-post-type-name',
));
function _zz(){
    $bq_content=_read('rp-qj-zz');
		$bq_descriptions = get_post_meta( get_the_ID(), 'rp-zz', true );
		if(!empty($bq_descriptions)){
		$descriptions = '<span>摄影.'.$bq_descriptions.'</span>';
		}else if(!$bq_descriptions && !$bq_content){
		 $descriptions = '';
		}else{
		$descriptions = '<span>摄影.'.$bq_content.'</span>';
		}
		echo $descriptions;
}
function _wenzhangbanquan(){
    $bq_content=_read('zero-article-bq');
		$bq_descriptions = get_post_meta( get_the_ID(), 'opt-wz-textbanquan', true );
		if(!empty($bq_descriptions)){
		$descriptions = $bq_descriptions;
		} else{
		$descriptions = $bq_content;
		}
		echo $descriptions;
}
function catch_first_image() {
    global $post, $posts;$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/iU', $post->post_content, $matches);
	@$first_img = $matches [1] [0];
	if(empty($first_img)){
		$random = mt_rand(1, 3);
		echo get_bloginfo ( 'stylesheet_directory' );
		echo '/images/random/'.$random.'.jpg';
		}
  return $first_img;
}
function post_thumbnail_src(){
	global $post;
	if( $values = get_post_custom_values("thumb") ) {
		$values = get_post_custom_values("thumb");
		$post_thumbnail_src = $values [0];
	} elseif( has_post_thumbnail() ){ 
		$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
		$post_thumbnail_src = $thumbnail_src [0];
	} else {
		$post_thumbnail_src = '';
		ob_start();
		ob_end_clean();
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		if(!empty($matches[1][0])){
			$post_thumbnail_src = $matches[1][0]; 
		}else{
			$post_thumbnail_src = 1;
				$random = mt_rand(1, 3);
			$post_thumbnail_src = get_template_directory_uri().'/images/random/'.$random.'.jpg';
		}
	};
	return $post_thumbnail_src;
}
function image_tspd(){
$gettesheimg = wp_get_attachment_image_src( get_post_thumbnail_id());
if(!empty($gettesheimg)){
    echo $gettesheimg[0];
    
}else{
    echo catch_first_image();
}}
function example_theme_qtlogo() {
    $htkglogo = _read('zero-ht-logokg');
	if(!empty($htkglogo)){
     $qtwz='<span>'. _read('rp-logo-wz') .'</span>';
     $yanshe= '15px';
   }else{
     $qtwz='<img src="'. _read('rp-logo-img') .'"  alt="'.get_bloginfo('name').'">';
   }
echo $qtwz;
}
function _rp_nav_img(){
    if(_read('rp-nav-img')){
        $img='background-image: url('._read('rp-nav-img').');';
    }else{
        $img='';
    }
    echo $img;
}
function _rp_tw_wzzz(){
    $wzzz = get_post_meta( get_the_ID(), 'rp-wzzz', true );
    if($wzzz){
        $wzzz=$wzzz;
    }elseif(_read('rp-tw-dwzz')){
        $wzzz=_read('rp-tw-dwzz');
    }else{
         $wzzz=post_views('',' 浏览');
    }
    echo $wzzz;
}
function _rp_tw_imgzz(){
    $wzzz = get_post_meta( get_the_ID(), 'rp-imgzz', true );
    if($wzzz){
        $wzzz='摄影 | '.$wzzz;
    }elseif(_read('rp-tw-imgzz')){
        $wzzz='摄影 | '._read('rp-tw-imgzz');
    }else{
         $wzzz='';
    }
    echo $wzzz;
}
function _rp_tw_nr_imgzz(){
    $wzzz = get_post_meta( get_the_ID(), 'rp-imgzz', true );
    if($wzzz){
        $wzzz='摄影 | '.$wzzz;
    }else{
         $wzzz= post_views('',' views');
    }
    echo $wzzz;
}
function _rp_nr_bottom_tool_kg(){
    $wzzz = _read('rp-bottom_tool_kg');
    if($wzzz){
        	include(TEMPLATEPATH . '/page/bottom-tool.php');
    }
}
add_filter('sanitize_file_name','fanly_custom_upload_name', 5, 1 );
function fanly_custom_upload_name($file){
    $info   = pathinfo($file);
    $ext    = empty($info['extension']) ? '' : '.' . $info['extension'];
    $name   = basename($file, $ext);
    if(preg_match("/[一-龥]/u",$file)){
        $file   = substr(md5($name), 0, 20) . rand(00,99) . $ext;
    }elseif(is_numeric($name)){
        $file   = substr(md5($name), 0, 20) . rand(00,99) . $ext;
    }
    return $file;
}
add_filter( 'pre_option_link_manager_enabled', '__return_true' );
function rp_nav_hei() {echo "<link href=".get_template_directory_uri()."/assets/modu/hei.css rel='stylesheet'>";}
if(!empty(_read('rp-header-color-box')=='2')){add_action( 'wp_head', 'rp_nav_hei' );}
function og(){
    if(is_single() && _read('rp-article-og-switch')){
        include(TEMPLATEPATH . '/inc/og.php');
    }
    
}
function plc_comment_post( $incoming_comment ) {
        $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
        $incoming_comment['comment_content'] = str_replace( "'",'', $incoming_comment['comment_content'] );
        return( $incoming_comment );
}
function plc_comment_display( $comment_to_display ) {
        $comment_to_display = str_replace( '', "'", $comment_to_display );
        return $comment_to_display;
}
function xintheme_prohibit_comment_post( $incoming_comment ) {
    $pattern = '/[一-龥]/u';
    if(!preg_match($pattern, $incoming_comment['comment_content'])) {
    wp_die( "抱歉，本站禁止全英文评论，请输入一些汉字，谢谢！<a href=\"#\" onclick=\"javascript:history.back(-1);\">返回到上一页</a>" );
    }
    return( $incoming_comment );
}
if (!empty(_read('rp-pl-comment'))) {
add_filter('preprocess_comment', 'xintheme_prohibit_comment_post');
}
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);
add_filter( 'comment_text', 'plc_comment_display', '', 1);
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);
function exclude_category_home( $query ) {
    $sadads=_read('zero-qt-index-paichuid');
    if ( $query->is_home ) {
        $query->set( 'cat', ''.$sadads.'' );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );